package leetcode;

import java.util.HashMap;

public class sum {

    public static void main(String[] args) {
        sum sum = new sum();
        int [] arr = sum.twoSum2(new int[]{1, 2, 3, 4,5 , 6, 7, 8}, 10);
        int [] arr1 = sum.twoSum1(new int[]{1, 2, 3, 4,5 , 6, 7, 8}, 10);
        System.out.println(arr);
    }
    public int[] twoSum1(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target) return new int[]{i, j};
            }
        }
        return null;
    }

    public int[] twoSum2(int[] nums, int target) {

        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(target - nums[i])){
                return new int[]{map.get(target - nums[i]), i};
            }else{
                map.put(nums[i], i);
            }
        }

        return null;
    }

}
